package com.zzxy.spring12.factory;

import com.zzxy.spring12.Aspect.Myaspect;
import com.zzxy.spring12.service.IStudentService;
import com.zzxy.spring12.service.impl.StudentServiceImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyBeanFactoryCglib {
    public static IStudentService createStudentService(){
        StudentServiceImpl studentServiceImpl =new StudentServiceImpl();

        Myaspect myAspect=new Myaspect();

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(studentServiceImpl.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                myAspect.before();
                Object obj=methodProxy.invokeSuper(o,objects);
                myAspect.after();
                return obj;
            }
        });
        return (StudentServiceImpl)enhancer.create();
    }
}
